package eerbaar.shared.internal.persist.load;

import eerbaar.shared.exceptions.GameException;
import eerbaar.shared.internal.persist.PersistService;
import eerbaar.shared.persist.IPersister;
import eerbaar.shared.tree.INode;


public class ObjectFactory {

    public <T> T create(Class<T> type, INode start) throws GameException {
        assert (start != null);

        String name = getClassName(start);
        IPersister<T> factory = PersistService.service().getPersister(type,
                name);
        if (factory == null) {
            throw new GameException(name + " not registered.");
        }

        return factory.load(start);
    }

    private String getClassName(INode start) throws GameException {

        MyNodeDecorator decorator = new MyNodeDecorator(start);
        return decorator.getPrivateClassName();
    }

}
